import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class GenerateDB {
	private AgesOfStrifeServer parent;
	private int villagesPerRuler;
	private int maxRulers;
	private int maxX;
	private int averageDistance;
	private Statement stmt, stmtUpdate;
//	private Connection con;
//	private String[] rulers;

	public GenerateDB( AgesOfStrifeServer parent ) {
		villagesPerRuler = 3;
		maxRulers = 500;
		averageDistance = 20;
		maxX = 160; // maxRulers * villagesPerRuler * averageDistance;
		this.parent = parent;
	}

	public void generateNewGame() {
		try {
			Connection con = getConnection();

			int previousX = 0;
			int previousY = 0;

			for ( int i = 0; i < maxRulers; i++ ) {
				int population = 5000;
				int foodQuality = 50;
				int ironQuality = 50;
				int coalQuality = 50;

				previousX += ( int ) ( Math.random() * averageDistance )
						+ averageDistance / 2;
				if ( previousX > maxX ) {
					previousX -= maxX;
					previousY++;
				}
				stmt = con.createStatement();
				stmt
						.execute( "INSERT INTO village ( population, x, y, food_quality, iron_quality, coal_quality, start_place, space )"
								+ " VALUES ( "
								+ population
								+ ", "
								+ previousX
								+ ", "
								+ previousY
								+ ", "
								+ foodQuality
								+ ", "
								+ ironQuality
								+ ", "
								+ coalQuality
								+ ", 1, 50 )" );
				for ( int p = 0; p < villagesPerRuler - 1; p++ ) {
					previousX += ( int ) ( Math.random() * 4 )
							+ averageDistance - 2;
					if ( previousX > maxX ) {
						previousX -= maxX;
						previousY++;
					}
					int space = 0;
					int catagory = ( int ) ( Math.random() * 3 );
					population = ( int ) ( Math.random() * 200 ) + 1100;
					if ( catagory == 0 ) {
						space = ( int ) ( Math.random() * 20 ) + 30;
						foodQuality = ( int ) ( Math.random() * 50 ) + 25;
						coalQuality = ( int ) ( Math.random() * 50 ) + 25;
						ironQuality = ( int ) ( Math.random() * 50 ) + 25;
					} else if ( catagory == 1 ) {
						space = ( int ) ( Math.random() * 30 ) + 40;
						foodQuality = ( int ) ( Math.random() * 35 ) + 45;
						coalQuality = ( int ) ( Math.random() * 35 ) + 45;
						ironQuality = ( int ) ( Math.random() * 35 ) + 45;
					} else {
						space = ( int ) ( Math.random() * 50 ) + 50;
						foodQuality = ( int ) ( Math.random() * 35 ) + 65;
						coalQuality = ( int ) ( Math.random() * 35 ) + 65;
						ironQuality = ( int ) ( Math.random() * 35 ) + 65;
					}
					stmt = con.createStatement();
					stmt
							.execute( "INSERT INTO village ( population, x, y, food_quality, iron_quality, coal_quality, start_place, space )"
									+ " VALUES ( "
									+ population
									+ ", "
									+ previousX
									+ ", "
									+ previousY
									+ ", "
									+ foodQuality
									+ ", "
									+ ironQuality
									+ ", "
									+ coalQuality + ", 0, " + space + " )" );
				}
			}
			stmt = con.createStatement();
			ResultSet resultSet = stmt
					.executeQuery( "SELECT village_id, start_place FROM village" );
			while ( resultSet.next() ) {
				int village_id = resultSet.getInt( "village_id" );
				int start_place = resultSet.getInt( "start_place" );

				stmtUpdate = con.createStatement();
				stmtUpdate
						.execute( "INSERT INTO village_buildings ( village_id, building_id, ammount ) VALUES ( "
								+ village_id + ", 0, 1 )" );

				if ( start_place == 1 ) {
					stmtUpdate
							.execute( "INSERT INTO village_buildings ( village_id, building_id, ammount ) VALUES ( "
									+ village_id + ", 1, 4 )" );
					stmtUpdate
							.execute( "INSERT INTO village_buildings ( village_id, building_id, ammount ) VALUES ( "
									+ village_id + ", 5, 1 )" );
					stmtUpdate
							.execute( "INSERT INTO village_buildings ( village_id, building_id, ammount ) VALUES ( "
									+ village_id + ", 4, 1 )" );
					stmtUpdate
							.execute( "INSERT INTO village_buildings ( village_id, building_id, ammount ) VALUES ( "
									+ village_id + ", 8, 1 )" );
				}
			}
		} catch ( Exception e ) {
			AgesOfStrifeServer.log.error( e );
			e.printStackTrace();
		}
	}

	public void clearDatabase() {
		try {
			Connection con = getConnection();

			stmt = con.createStatement();
			stmt.execute( "DELETE FROM ruler" );
			stmt.execute( "DELETE FROM village" );
			stmt.execute( "DELETE FROM village_queue" );
			stmt.execute( "DELETE FROM village_buildings" );
			stmt.execute( "DELETE FROM army" );
			stmt.execute( "DELETE FROM army_visible" );
			stmt.execute( "DELETE FROM army_orders" );
			stmt.execute( "DELETE FROM news" );
			stmt.execute( "DELETE FROM scouted" );
			stmt.execute( "DELETE FROM officers" );
			stmt.execute( "DELETE FROM upgrade_status" );
			stmt.execute( "DELETE FROM faction" );
			stmt.execute( "DELETE FROM faction_member" );
			stmt.execute( "DELETE FROM messages" );
			stmt.execute( "DELETE FROM message_ruler" );
			stmt.execute( "DELETE FROM ruler_expedition" );
			stmt.execute( "DELETE FROM trade" );
			stmt.execute( "DELETE FROM upgrade_status" );
			stmt.execute( "DELETE FROM transfer" );
			stmt.execute( "DELETE FROM colony" );
			stmt.execute( "DELETE FROM colony_transport" );
			stmt.execute( "DELETE FROM buddy WHERE accept1 = 0 OR accept2 = 0" );
			stmt.execute( "DELETE FROM races" );
			stmt.execute( "DELETE FROM race_teams" );
			stmt.execute( "DELETE FROM race_history" );
			stmt.execute( "DELETE FROM race_bets" );
			stmt.execute( "DELETE FROM history" );
			stmt.execute( "DELETE FROM history_rating" );
			stmt.execute( "DELETE FROM history_seen" );
			stmt.execute( "DELETE FROM chat" );
			stmt.execute( "DELETE FROM locations" );
			stmt.execute( "DELETE FROM ruler_task" );
			
			stmt.execute( "UPDATE game SET turn = 1, date = '01-03-1760', coal_gold = '0.2', iron_gold = '0.4', food_gold = '0.1', prizes_awarded = 0, special_event_initialized = 0, special_event_started = 0, season = 1, spotlight_village_id = -1 WHERE turn > 1" );
			stmt.execute( "UPDATE map_settings SET sector = 0, x = -40, y = 0" );
			stmt.execute( "UPDATE village_names SET used = 0" );
			stmt.execute( "UPDATE upgrades SET discount_turn = -1, discount_date = ''" );
		} catch ( Exception e ) {
			AgesOfStrifeServer.log.error( e );
			e.printStackTrace();
		}
	}

	public Connection getConnection() throws ClassNotFoundException,
			SQLException {
		Class.forName( "com.mysql.jdbc.Driver" );
		String url = "jdbc:mysql://" + parent.getDatabaseLocation()
				+ ":" + parent.getDatabasePort() + "/" + parent.getDatabaseName();
		return DriverManager.getConnection( url, parent.getDatabaseUser(), parent.getDatabasePassword() );
	}
}
